package com.xinyue.game.message;

import java.nio.charset.StandardCharsets;

import com.xinyue.game.utils.ByteBufUtil;

import io.netty.buffer.ByteBuf;
import lombok.Data;

/**
 * @author 王广帅
 * @since 2023/7/2 13:25
 **/
@Data
public class GameLogicMessageHeader extends GameGatewayMessageHeader {

    private short gatewayId;
    // 客户端的ip地址
    private String clientIp;

    private byte[] clientIpBytes;

    @Override
    public void readHeader(ByteBuf byteBuf) {
        super.readHeader(byteBuf);
        this.gatewayId = byteBuf.readShort();
        this.clientIp = ByteBufUtil.readString(byteBuf);
    }

    @Override
    public int getTotalSize() {
        this.clientIpBytes = clientIp.getBytes(StandardCharsets.UTF_8);
        return super.getTotalSize() + 2 + this.clientIpBytes.length;
    }

    @Override
    public void writeHeader(ByteBuf byteBuf) {
        super.writeHeader(byteBuf);
        byteBuf.writeShort(gatewayId);
        byteBuf.writeInt(this.clientIpBytes.length);
        byteBuf.writeBytes(this.clientIpBytes);
    }
}
